package com.ldzy.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ldzy.domain.*;
import com.ldzy.service.ToolService;
import com.ldzy.service.impl.ToolServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ToolController {

    @Autowired
    private ToolService toolService;

    @RequestMapping("/selectNewbooks")//工具管理系统页面控制器
    public String selectNewbooks(Model model) {
        PageHelper.startPage(1, 10);
        Page<Tool> tool = toolService.findByBookWithUpLoadTimeDesc(null);
        model.addAttribute("pageResult", tool.getResult());
        return "admin/tool_new";
    }

    @RequestMapping("/returnConfirm")
    @ResponseBody
    public Result returnConfirm(Tool tool, HttpSession session) {
        //确认归还
        //修改书的状态为0
//        修改书的借阅人，借阅时间，预计归还时间为空：
//        到借阅记录表中插入一条记录：

        tool.setStatus("0");
        tool.setBorrower(null);
        tool.setBorrowtime(null);
        tool.setReturntime(null);

        int i = toolService.returnConfirm(tool);
        if (i > 0) {
            return new Result(true, ResultCode.SUCCESS.getDesc(), ResultCode.SUCCESS.getCode());
        }
        return new Result(false, ResultCode.ERROR.getDesc(), ResultCode.ERROR.getCode());
    }

    @RequestMapping("/borrowBook")
    @ResponseBody
    public Result borrowBook(Tool tool, HttpSession session) {
        tool.setStatus("1");
        User user = (User) session.getAttribute("user");
        tool.setBorrower(user.getName());
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        tool.setBorrowtime(simpleDateFormat.format(date));
        int i = toolService.edit(tool);
        if (i > 0) {
            return new Result(true, ResultCode.SUCCESS.getDesc(), ResultCode.SUCCESS.getCode());
        }
        return new Result(false, ResultCode.ERROR.getDesc(), ResultCode.ERROR.getCode());
    }

    @RequestMapping("/returnBook")
    @ResponseBody
    public Result returnBook(Tool tool, HttpSession session) {
        tool.setStatus("2");
        int i = toolService.edit(tool);
        if (i > 0) {
            return new Result(true, ResultCode.SUCCESS.getDesc(), ResultCode.SUCCESS.getCode());
        }
        return new Result(false, ResultCode.ERROR.getDesc(), ResultCode.ERROR.getCode());
    }

    @RequestMapping("/search")//工具借用页面控制器
    public ModelAndView search(ModelAndView modelAndView,String name ) {
        List<Tool> clothingList = toolService.findbyName(name);
        modelAndView.setViewName("admin/books");
        modelAndView.addObject("clothingList", clothingList);
        return modelAndView;
//    @RequestMapping("/searchBorrowed")//当前借用页面控制器
//    public String searchBorrowed() {
//        return "forward:/admin/tool_borrowed.jsp";
  }


    @RequestMapping("/searchBorrowed")
    public ModelAndView searchBorrowed(ModelAndView modelAndView,String name ) {
        List<Tool> clothingList = toolService.findByName(name);
        modelAndView.setViewName("admin/tool_borrowed");
        modelAndView.addObject("clothingList", clothingList);
        return modelAndView;

    }

    @RequestMapping("/searchRecords")//工具管理系统页面控制器
    public String searchRecords(Model model) {
        PageHelper.startPage(1, 10);
        Page<Record> records = toolService.findByBook(null);
        model.addAttribute("pageResult", records.getResult());
        return "admin/tool_record";
    }


}
